<!DOCTYPE html>
<html>
<head>
    <title>鹿鹿泽会员生日月份清单</title>
    <link rel="stylesheet" href="../static/css/minghu.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon">
</head>
<body style="background-color: rgb(254, 254, 255);">
    {% include 'show_ins_info.html' %}
    <hr>
    <h2 id="title" style="margin-bottom: 40px;color:#5bcf7e;">鹿鹿泽会员生日月份清单</h2>    

    <div>
        <!-- <hr style="border-top:1px dashed;margin-bottom: 30px;"> -->
        <div id="birthdayCusList"></div>
    </div>


    <script src="../static/js/common.js"></script>
    <script src="../static/js/echarts.min.js"></script>
    
    <script>
        let insName;
        let insId;
        let insRole;
        let insList;
        
        document.addEventListener('DOMContentLoaded',function(){
            // loginCheck();        
            const insInfo=hideInsSelectBlockAndGetInsInfo();
            insId=insInfo.sessionInsId;
            insName=insInfo.sessionInsName;
            insRole=insInfo.sessionInsRole;
            // console.log(insName)
            //教练角色不允许进入该页面，返回Index
            if(insRole==='ins'){
                window.location.replace('./index');
            }

            const today=new Date();

           
            fetch('/get_ins_list', {
                method: 'GET',
            })
            .then(response => response.json())
            .then(data => {
            //将data赋值给块变量cus_list，供后面的handleInput使用
                console.log('ins list:',data);
                insList=data['ins_list'];
            })
            .catch(error => console.error('Error:', error));
            

            showCusBirthdayList();
         

        });

        function showCusBirthdayList(){
            const birthdayCusList=document.getElementById('birthdayCusList');
            birthdayCusList.style.width='100%';
            birthdayCusList.style.marginBottom='20px';
            birthdayCusList.innerHTML='';

            let insIdPage;
            if(insRole==='ins'){
                insIdPage=insId;
            }else{
                //特殊，陆的情况
                if(insRole==='admin'){
                    if(insId==='LZADM002' || insId==='LZADM001'){
                        insIdPage='LZINS001';
                    }
                    
                }
            }


            fetch('/get_cus_birthday')
            .then(response=>response.json())
            .then(data=>{
                if(data['res']==='ok'){
                    const bthData=data['data'];
                    // console.log(data['data']);
                    //获取月份列表
                    let monthArr = [];
                    bthData.forEach(d => {
                        let month = new Date(d['birthday']).getMonth();
                        if (!monthArr.includes(month)) {
                            monthArr.push(month);
                        }
                    });

                    // 对月数组进行排序，让 1 月排在第一位
                    monthArr.sort((a, b) => a - b);

                    monthArr.forEach(month => {
                        const monthBox = document.createElement('div');
                        monthBox.id = month + "-box";
                        const monthTitle = document.createElement('div');
                        monthTitle.innerHTML = `${month + 1}月`;
                        monthTitle.id = month + '-title';
                        monthTitle.classList.add('birthday-month-title');
                        if(month===new Date().getMonth()){
                            monthTitle.style.backgroundColor='#fcb32b';
                            monthTitle.style.fontWeight='bold';
                        }
                        monthBox.appendChild(monthTitle);
                        monthBox.style.marginBottom='20px';
                        birthdayCusList.appendChild(monthBox);
                    });

                    monthArr.forEach(month=>{
                        const monthBox=document.getElementById(month + "-box");
                        let str='';
                        bthData.forEach(d=>{
                            const bthMonth=new Date(d['birthday']).getMonth();
                            const bthDate=new Date(d['birthday']).getDate();
                            const today=new Date();
                            const thisMonth=today.getMonth();
                            if(month===new Date(d['birthday']).getMonth()){        
                                // cusListDiv.id=bthMonth+'-str';                                
                                
                                if(month===new Date().getMonth()){
                                    str+=`<p><span style="color:#fcb32b;">· ${d['cus_name']}（${new Date(d['birthday']).getMonth()+1}月${new Date(d['birthday']).getDate()}日，${calculateAge(d['birthday'])}岁）</span> /&nbsp;&nbsp;<span style="color:#fcb32b;font-size:12px;">${d['ins_name']}</span></p>`;
                                }else{
                                    str+=`<p>· ${d['cus_name']}（${new Date(d['birthday']).getMonth()+1}月${new Date(d['birthday']).getDate()}日，${calculateAge(d['birthday'])}岁） /&nbsp;&nbsp;<span style="font-size:12px;">${d['ins_name']}</span></p>`;
                                }
                            }

                        });
                        // str=str.slice(0,-1);
                        const cusListDiv=document.createElement('div');
                        
                        cusListDiv.innerHTML=`${str}`;
                        cusListDiv.classList.add('birthday-month-str');
                        monthBox.appendChild(cusListDiv);
                    });



                }else{

                }
            })
            .catch(error=>console.log(error));

            
        }


                

        function calculateAge(birthday) {
            // 将生日字符串转换为 Date 对象
            const birthDate = new Date(birthday);
            const today = new Date();
            
            // 计算年差
            let age = today.getFullYear() - birthDate.getFullYear();
            
            // 检查是否还未过今年的生日
            // const monthDiff = today.getMonth() - birthDate.getMonth();
            // if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
            //     age--;
            // }
            
            return age;
        }


    </script>
  
</body>
</html>
